<?php

namespace App\Http\Controllers;

use App\Exceptions\ApiException;
use Auth;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Routing\Controller as BaseController;
use function response;

class Controller extends BaseController
{
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;

    public function __construct()
    {
        $this->middleware(['change.guard:'.$this->guard,'checkToken'],['except' => ['login','register']]);
        $this->user=Auth::user();
    }

    /**
     * 注销用户（使令牌无效）。
     *
     * @return \Illuminate\Http\JsonResponse
     */
    public function logout()
    {
        auth::logout();
        return $this->success();
    }

    public function success($data=[],$code=0,$msg="success")
    {
        return response()->json([
            'code' => $code,
            'msg'  => $msg,
            'data' => $data,
        ], 200);
    }

    public function error($msg,$code=1)
    {
        return response()->json([
            'code' => $code,
            'msg'  => $msg,
        ], 200);
    }

    public function throw_error($msg,$code=1)
    {
        throw new ApiException($msg,$code);
    }
}
